import java.util.Map;
import java.util.HashMap;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import java.net.URLEncoder;

public class vx
{
	private static String cookie = "";//全局更新cookie
	
	
	/*获取WEB微信的uuid*/
	public static String getUUID(){
		String url = "https://login.wx2.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx2.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=" + utils.time(false);
		Map<String,String> map = new HashMap<String,String>();
		map.put("Referer","https://wx2.qq.com/");
		netutils.setHeader(map);
		netutils.setTimeout(5000 * 2);
		String data = netutils._GET(url);
		//window.QRLogin.code = 200; window.QRLogin.uuid = "YaIKYXPfYA==";
		if(utils.getSubstr(data,"window.QRLogin.code = ",";").equals("200")){
			return utils.getSubstr(data,"window.QRLogin.uuid = \"","\";");
		}else{
			return "";
		}
	}
	
	/*获取登录二维码*/
	public static String getQRcode(String uuid){
		return "https://login.weixin.qq.com/qrcode/" + uuid;
	}
	
	/*获取二维码状态*/
	public static Map<String,Object> getQRst(String uuid){
		String url = "https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=" + uuid + "&tip=0&r=" + utils.ranNum(9) + "&_=" + utils.time(false);
		netutils.setTimeout(1000 * 26);
		String data = netutils._GET(url);
		int code = Integer.parseInt(utils.getSubstr(data,"window.code=",";"));
		if(code == 408){//二维码未扫描
			Map<String,Object> re = new HashMap<String,Object>();
			re.put("code",408);
			return re;
		}else if(code == 201){//二维码已扫描，未确认登录
			String icon = utils.getSubstr(data,"window.userAvatar = '","';");
			Map<String,Object> re = new HashMap<String,Object>();
			re.put("code",201);
			re.put("icon",icon);
			return re;
		}else if(code == 200){//已确认登录
			String uri = utils.getSubstr(data,"window.redirect_uri=\"","\";");
			Map<String,Object> re = new HashMap<String,Object>();
			re.put("code",200);
			re.put("uri",uri);
			return re;
		}else{//二维码已过期或其它
			Map<String,Object> re = new HashMap<String,Object>();
			re.put("code",400);
			return re;
		}
	}
	
	/*登录获得数据*/
	public static Map<String,Object> loginGetData(String uri){
		String url = uri + "&fun=new&version=v2";
		netutils.setTimeout(5000 * 2);
		String data = netutils._GET(url);
		String ret = utils.getSubstr(data,"<ret>","</ret>");
		if(ret.equals("0")){
			cookie = netutils.getCookie();
			netutils.setCookie(cookie);
			String skey = utils.getSubstr(data,"<skey>","</skey>");
			String wxsid = utils.getSubstr(data,"<wxsid>","</wxsid>");
			String wxuin = utils.getSubstr(data,"<wxuin>","</wxuin>");
			String pass_ticket = utils.getSubstr(data,"<pass_ticket>","</pass_ticket>");
			Map<String,Object> re = new HashMap<String,Object>();
			re.put("code",0);
			re.put("skey",skey);
			re.put("wxsid",wxsid);
			re.put("wxuin",wxuin);
			re.put("pass_ticket",pass_ticket);
			return re;
		}else{
			Map<String,Object> re = new HashMap<String,Object>();
			re.put("code",-1);
			re.put("message",utils.getSubstr(data,"<message>","</message>"));
			return re;
		}
	}
	
	/*初始化二次登录*/
	public static String login(String skey,String wxuin,String wxsid,String pass_ticket){
		String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=" + utils.ranNum(9) + "&pass_ticket=" + pass_ticket;
		String data = "{\"BaseRequest\":{\"Uin\":\"" + wxuin + "\",\"Sid\":\"" + wxsid + "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"e" + utils.ranNum(15) + "\"}}";
		netutils.setTimeout(5000 * 2);
		netutils.setCookie(cookie);
		data = netutils._POST(url,data);
		return data;
		
	}
	
	/*开启微信状态通知*/
	public static boolean onstatus(String skey,String wxuin,String wxsid,String pass_ticket,String UserName){
		String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?lang=zh_CN&pass_ticket=" + pass_ticket;
		String data = "{\"BaseRequest\":{\"Uin\":" + wxuin + ",\"Sid\":\"" + wxsid + "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"e" + utils.ranNum(15) + "\"},\"Code\":3,\"FromUserName\":\"" + UserName + "\",\"ToUserName\":\"" + UserName + "\",\"ClientMsgId\":" + utils.time(false) + "}";
		netutils.setTimeout(5000 * 2);
		netutils.setCookie(cookie);
		data = netutils._POST(url, data);
		try{
			JSONObject json = new JSONObject(data);
			if (json.getJSONObject("BaseResponse").getInt("Ret") == 0)
			{
				return true;
			}else{
				return false;
			}
		}catch (JSONException e){
			return false;
		}

	}
	
	/*获取联系人列表*/
	public static String getContact(String skey,String pass_ticket){
		String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket=" + pass_ticket + "&r=" + utils.time(false) + "&seq=0&skey=" + skey;
		netutils.setTimeout(5000 * 2);
		netutils.setCookie(cookie);
		String data = netutils._GET(url);
		try{
			JSONObject json = new JSONObject(data);
			if (json.getJSONObject("BaseResponse").getInt("Ret") == 0){
				return data;
			}else{
				return "";
			}
		}catch (JSONException e){
			return "";
		}
	}
	
	/*心跳包*/
	public static boolean push(String skey,String wxuin,String wxsid,String synckey){
		String url = "https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck?r=" + utils.time(false) + "&skey=" + skey + "&sid=" + wxsid + "&uin=" + wxuin + "&deviceid=e" + utils.ranNum(15) + "&synckey=" + URLEncoder.encode(synckey);
		netutils.setTimeout(1000 * 26);
		netutils.setCookie(cookie);
		String data = netutils._GET(url);
		if(Integer.parseInt(utils.getSubstr(data,"retcode:\"","\"")) == 0){
			if(Integer.parseInt(utils.getSubstr(data,"selector:\"","\"")) == 2){
				return true;
			}
		}
		return false;
	}
	
	/*获取新消息*/
	public static String getmsg(String skey,String wxuin,String wxsid,String pass_ticket,String synckey){
		String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=" + wxsid + "&skey=" + skey + "&pass_ticket=" + pass_ticket;
		//1_688655|2_889998
		//{"Key":1,"Val":685555185},{"Key":2,"Val":685555185}
		int count = synckey.split("\\|").length;
		synckey = "{\"Key\":" + synckey.replaceAll("_",",\"Val\":").replace("|","},{\"Key\":") + "}";
		String data = "{\"BaseRequest\":{\"Uin\":" + wxuin + ",\"Sid\":\"" + wxsid + "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"e" + utils.ranNum(15) + "\"},\"SyncKey\":{\"Count\":" + count + ",\"List\":[" + synckey + "]},\"rr\":" + utils.ranNum(9) + "}";
		netutils.setTimeout(5000 * 2);
		netutils.setCookie(cookie);
		data = netutils._POST(url,data);
		
		try{
			JSONObject json = new JSONObject(data);
			if (json.getJSONObject("BaseResponse").getInt("Ret") == 0){
				return data;
			}else{
				return "";
			}
		}catch (JSONException e){
			return "";
		}
	}
	
	/*发送文本消息*/
	public static boolean sendtextmsg(String skey,String wxuin,String wxsid,String pass_ticket,String FromUserName,String ToUserName,String msg){
		String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?lang=zh_CN&pass_ticket=" + pass_ticket;
		String data = "{\"BaseRequest\":{\"Uin\":" + wxuin + ",\"Sid\":\"" + wxsid + "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"e" + utils.ranNum(15) + "\"},\"Msg\":{\"Type\":1,\"Content\":\"" + msg + "\",\"FromUserName\":\"" + FromUserName + "\",\"ToUserName\":\"" + ToUserName + "\",\"LocalID\":\"15501462659210401\",\"ClientMsgId\":\"15501462659210401\"},\"Scene\":0}";
		netutils.setTimeout(5000 * 2);
		netutils.setCookie(cookie);
		data = netutils._POST(url,data);
		try{
			JSONObject json = new JSONObject(data);
			if(json.getJSONObject("BaseResponse").getInt("Ret") == 0){
				return true;
			}else{
				return false;
			}
		}catch (JSONException e){
			return false;
		}
	}
	/*
	private static String r(){
		return String.valueOf(Integer.parseInt(utils.time(false)) / 60 / 60);
	}
	*/
}
